/**
 * 
 */
package cn.com.xf.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import cn.com.xf.common.Constants;
import cn.com.xf.dao.TblCheckCmdDAO;
import cn.com.xf.dao.TblCheckDeviceDAO;
import cn.com.xf.dao.TblOaAuthPermissionDAO;
import cn.com.xf.dao.TblOaAuthRoleDAO;
import cn.com.xf.dao.TblOaAuthRolePermissionDAO;
import cn.com.xf.dao.TblOaAuthUserDAO;
import cn.com.xf.dao.TblOaAuthUserRoleDAO;
import cn.com.xf.dao.TsUserDAO;
import cn.com.xf.domain.TblCheckCmd;
import cn.com.xf.domain.TblCheckDevice;
import cn.com.xf.domain.TblOaAuthPermission;
import cn.com.xf.domain.TblOaAuthRole;
import cn.com.xf.domain.TblOaAuthRolePermission;
import cn.com.xf.domain.TblOaAuthUser;
import cn.com.xf.domain.TblOaAuthUserRole;
import cn.com.xf.domain.TsUser;
import cn.com.xf.service.SettingService;
import cn.com.xf.utils.encode.EncrypTool;
import cn.com.xf.web.form.LeaveForm;
import cn.com.xf.web.form.SettingForm;

/**
 * @author Administrator
 * 
 */
@Service
public class SettingServiceImpl implements SettingService {

	@Autowired
	private TblOaAuthRoleDAO tblOaAuthRoleDAO;
	@Autowired
	private TsUserDAO tsUserDAO;
	@Autowired
	private TblOaAuthUserRoleDAO tblOaAuthUserRoleDAO;
	@Autowired
	private TblOaAuthUserDAO tblOaAuthUserDAO;
	@Autowired
	private TblOaAuthPermissionDAO tblOaAuthPermissionDAO;
	@Autowired
	private TblOaAuthRolePermissionDAO tblOaAuthRolePermissionDAO;
	
	@Autowired
	private TblCheckDeviceDAO tblCheckDeviceDAO;
	
	@Autowired
	private TblCheckCmdDAO tblCheckCmdDAO;

	@Override
	public List<TblOaAuthRole> selectRoleList() {
		return tblOaAuthRoleDAO.selectRoleList();
	}

	@Override
	public List<TblOaAuthRole> selectRoleList4Check(String siteId) {
		return tblOaAuthRoleDAO.selectRoleList4Check(siteId);
	}

	@Override
	public int insertUser(SettingForm form) {
		// 1.插入ts_user
		TsUser tsUser = new TsUser();
		tsUser.setUserid(form.getUserId());
		tsUser.setUsername(form.getUserName());
		tsUser.setPassword("C5E4C16E03EA");
		tsUser.setSiteid("769002");
		tsUser.setEnabled(new BigDecimal(0));
		tsUser.setCreatedate(new Date());
		tsUser.setCreateDateStr(DateFormatUtils.format(tsUser.getCreatedate(), "yyyy-MM-dd HH:mm:ss"));
		tsUserDAO.insert(tsUser);
		// 2.插入auth_user
		TblOaAuthUser authUser = new TblOaAuthUser();
		authUser.setUsername(form.getUserId());
		authUser.setEnabled("1");
		authUser.setAddTime(new Date());
		String name = SecurityContextHolder.getContext().getAuthentication().getName();
		authUser.setAddUser(name);
		int bizId = 0;
		bizId = tblOaAuthUserDAO.insert(authUser);
		// 3.插入auth_user_role可能有多条记录产生
		String roles = form.getRoles();
		String roleArr[] = roles.split(",");
		for (String role : roleArr) {
			String realRole = EncrypTool.dencode(role);
			TblOaAuthUserRole userRole = new TblOaAuthUserRole();
			userRole.setBizId(new BigDecimal(bizId));
			userRole.setRoleId(realRole);
			tblOaAuthUserRoleDAO.insert(userRole);
		}
		return 0;
	}

	@Override
	public List<TsUser> selectUserList(String orderByField, String orderByMode, String userId, String userName,
			String siteId, String deptId, String enabled, String createDateBegin, String createDateEnd, int begin,
			int pageSize) {
		// 1.获取账号的基本信息
		List<TsUser> list = tsUserDAO.selectUserList(orderByField, orderByMode, userId, userName, siteId, deptId,
				enabled, createDateBegin, createDateEnd, begin, pageSize);
		List<String> userNameList = new ArrayList<String>();
		if (!CollectionUtils.isEmpty(list)) {
			for (TsUser user : list) {
				userNameList.add(user.getUserid());
				user.setRoleDescs("");
				if (null != user.getEnabled()) {
					if ((user.getEnabled() + "").equals("0")) {
						user.setEnableHanzi(Constants.TS_USER_ENABLED_NO);
					} else if ((user.getEnabled() + "").equalsIgnoreCase("1")) {
						user.setEnableHanzi(Constants.TS_USER_ENABLED_YES);
					}
				}
			}
		}

		// 2.获取账户的角色
		List<TblOaAuthUser> roleList = tblOaAuthUserDAO.selectRoleByUserNames(userNameList);
		if (!CollectionUtils.isEmpty(list) && !CollectionUtils.isEmpty(roleList)) {
			for (TsUser user : list) {
				String roleDescs = "";
				for (TblOaAuthUser roleUser : roleList) {
					if (user.getUserid().equalsIgnoreCase(roleUser.getUsername())) {
						if (StringUtils.isNotBlank(roleUser.getRoleDesc())) {
							roleDescs = roleDescs + "," + roleUser.getRoleDesc();
						}
					}
				}
				roleDescs = roleDescs.replaceFirst(",", "");
				user.setRoleDescs(roleDescs);
			}
		}
		return list;
	}

	@Override
	public int selectUserListCount(String userId, String userName, String siteId, String deptId, String enabled,
			String createDateBegin, String createDateEnd) {
		return tsUserDAO.selectUserListCount(userId, userName, siteId, deptId, enabled, createDateBegin, createDateEnd);
	}

	@Override
	public int deleteUserByUserId(String userId) {
		TblOaAuthUser user = tblOaAuthUserDAO.selectByUserName(userId);
		if (null == user)
			return -1;
		// 1.删掉ts_user
		int i = tsUserDAO.deleteByPrimaryKey(userId);
		// 2.删掉auth_user_role
		int i2 = tblOaAuthUserRoleDAO.deleteByBizId(user.getBizId() + "");
		// 3.删除auth_user
		int i3 = tblOaAuthUserDAO.deleteByUserName(userId);
		return 0;
	}

	@Override
	public TsUser selectByUserId(String userId) {
		return tsUserDAO.selectByPrimaryKey(userId);
	}

	@Override
	public TsUser selectUserWithRoleByUserId(String userId) {
		TsUser tsUser = tsUserDAO.selectByPrimaryKey(userId);
		List<String> userNameList = new ArrayList<String>();
		if (null != tsUser) {
			userNameList.add(tsUser.getUserid());
		}

		if (CollectionUtils.isEmpty(userNameList)) {
			return tsUser;
		}

		// 2.获取账户的角色
		List<TblOaAuthUser> roleList = tblOaAuthUserDAO.selectRoleByUserNames(userNameList);
		if (!CollectionUtils.isEmpty(roleList)) {
			String roleDescs = "";
			String roleIds = "";
			for (TblOaAuthUser roleUser : roleList) {
				if (tsUser.getUserid().equalsIgnoreCase(roleUser.getUsername())) {
					if (StringUtils.isNotBlank(roleUser.getRoleDesc())) {
						roleDescs = roleDescs + "," + roleUser.getRoleDesc();
						roleIds = roleIds + "," + roleUser.getRoleId();
					}
				}
			}
			roleDescs = roleDescs.replaceFirst(",", "");
			roleIds = roleIds.replaceFirst(",", "");
			tsUser.setRoleDescs(roleDescs);
			tsUser.setRoleIds(roleIds);
		}
		return tsUser;
	}

	@Override
	public int updateUser(SettingForm form) {
		String loginName = SecurityContextHolder.getContext().getAuthentication().getName();
		String userId = form.getUserId();
		String userName = form.getUserName();
		// 1.更新ts_user
		// TsUser tsUser = tsUserDAO.selectByPrimaryKey(userId);
		// tsUser.setUsername(userName);
		// tsUser.setModifytime(new Date());
		// tsUser.setModifyuser(loginName);
		// tsUserDAO.updateByPrimaryKeySelective(tsUser);

		// 2.获取biz_id
		BigDecimal biz_id = null;
		List<String> userNameList = new ArrayList<String>();
		userNameList.add(form.getUserId());
		List<TblOaAuthUser> existAuthUserList = tblOaAuthUserDAO.selectRoleByUserNames(userNameList);
		if (CollectionUtils.isEmpty(existAuthUserList)) {
			// 增加auth_user
			TblOaAuthUser authUser = new TblOaAuthUser();
			authUser.setUsername(form.getUserId());
			authUser.setEnabled("1");
			authUser.setAddTime(new Date());
			authUser.setAddUser(loginName);
			int bizId = 0;
			bizId = tblOaAuthUserDAO.insert(authUser);
			biz_id = new BigDecimal(bizId);
		} else {
			biz_id = existAuthUserList.get(0).getBizId();
		}

		// 3.获取都包含的角色
		List<String> bothRoles = new ArrayList<String>();
		String newRoles = form.getRoles();
		String newRoleArr[] = newRoles.split(",");

		for (String role : newRoleArr) {
			String realRole = EncrypTool.dencode(role);
			for (TblOaAuthUser existRole : existAuthUserList) {
				if (realRole.equalsIgnoreCase(existRole.getRoleId() + "")) {
					bothRoles.add(realRole);
				}
			}
		}

		// 4.增加的
		for (String role : newRoleArr) {
			String realRole = EncrypTool.dencode(role);
			if (!bothRoles.contains(realRole)) {
				TblOaAuthUserRole userRole = new TblOaAuthUserRole();
				userRole.setBizId(biz_id);
				userRole.setRoleId(realRole);
				tblOaAuthUserRoleDAO.insert(userRole);
			}
		}

		// 5.减少的
		for (TblOaAuthUser u : existAuthUserList) {
			if (!bothRoles.contains(u.getRoleId() + "")) {
				tblOaAuthUserRoleDAO.deleteByBizIdAndRoleId(biz_id + "", u.getRoleId() + "");
			}
		}

		return 0;
	}

	/**
	 * 以auth_user为主导的更新。作废
	 * 
	 * @param form
	 * @return
	 */
	public int updateUser_authUser(SettingForm form) {
		String loginName = SecurityContextHolder.getContext().getAuthentication().getName();
		String userId = form.getUserId();
		String userName = form.getUserName();
		// 1.更新ts_user
		/*
		 * TsUser tsUser = tsUserDAO.selectByPrimaryKey(userId);
		 * tsUser.setUsername(userName); tsUser.setModifytime(new Date());
		 * tsUser.setModifyuser(loginName);
		 * tsUserDAO.updateByPrimaryKeySelective(tsUser);
		 */

		// 2.获取biz_id
		BigDecimal biz_id = null;
		List<String> userNameList = new ArrayList<String>();
		userNameList.add(form.getUserId());
		List<TblOaAuthUser> existAuthUserList = tblOaAuthUserDAO.selectRoleByUserNames(userNameList);
		// 增加auth_user
		/*
		 * TblOaAuthUser authUser = new TblOaAuthUser();
		 * authUser.setUsername(form.getUserId()); authUser.setEnabled("1");
		 * authUser.setAddTime(new Date()); authUser.setAddUser(loginName); int
		 * bizId = 0; bizId = tblOaAuthUserDAO.insert(authUser); biz_id = new
		 * BigDecimal(bizId);
		 */
		biz_id = existAuthUserList.get(0).getBizId();

		// 3.获取都包含的角色
		List<String> bothRoles = new ArrayList<String>();
		String newRoles = form.getRoles();
		String newRoleArr[] = newRoles.split(",");

		for (String role : newRoleArr) {
			String realRole = EncrypTool.dencode(role);
			for (TblOaAuthUser existRole : existAuthUserList) {
				if (realRole.equalsIgnoreCase(existRole.getRoleId() + "")) {
					bothRoles.add(realRole);

				}
			}
		}

		// 4.增加的
		for (String role : newRoleArr) {
			String realRole = EncrypTool.dencode(role);
			if (!bothRoles.contains(realRole)) {
				TblOaAuthUserRole userRole = new TblOaAuthUserRole();
				userRole.setBizId(biz_id);
				userRole.setRoleId(realRole);
				tblOaAuthUserRoleDAO.insert(userRole);
			}
		}

		// 5.减少的
		for (TblOaAuthUser u : existAuthUserList) {
			if (!bothRoles.contains(u.getRoleId() + "")) {
				tblOaAuthUserRoleDAO.deleteByBizIdAndRoleId(biz_id + "", u.getRoleId() + "");
			}
		}

		return 0;
	}

	@Override
	public List<TblOaAuthPermission> selectAllPremission() {
		List<TblOaAuthPermission> list =  tblOaAuthPermissionDAO.selectAllPermission();
		List<TblOaAuthPermission> permissions = new ArrayList<TblOaAuthPermission>();
		for (TblOaAuthPermission permission: list) {
			if (!permission.getPermissionTag().startsWith("CHECK_"))
				permissions.add(permission);
		}
		return permissions;
	}

	@Override
	public List<TblOaAuthPermission> selectAllPremission4Check() {
		return tblOaAuthPermissionDAO.selectAllPermission4Check();
	}

	@Override
	public int insertRole(SettingForm form) {
		// 1.插入角色表
		TblOaAuthRole role = new TblOaAuthRole();
		role.setRoleDesc(form.getRoleDesc().trim());
		int roleId = 0;
		roleId = tblOaAuthRoleDAO.insert(role);

		// 2.插入角色权限表
		String permissions = form.getPermissioins();
		String permissionArr[] = permissions.split(",");
		for (String s : permissionArr) {
			String realPermission = EncrypTool.dencode(s);
			TblOaAuthRolePermission rp = new TblOaAuthRolePermission();
			rp.setRoleId(new BigDecimal(roleId));
			rp.setPermissionId(new BigDecimal(realPermission));
			tblOaAuthRolePermissionDAO.insert(rp);
		}
		return 0;
	}

	@Override
	public int insertRole4Check(LeaveForm form) {
		// 1.插入角色表
		TblOaAuthRole role = new TblOaAuthRole();
		role.setRoleDesc(form.getRolename().trim());
		role.setSiteId(form.getSiteid().trim());
		role.setRoleName("CHECK_");
		int roleId = 0;
		roleId = tblOaAuthRoleDAO.insert(role);

		// 2.插入角色权限表
		String permissions = form.getPermissioins();
		String permissionArr[] = permissions.split(",");
		for (String s : permissionArr) {
			String realPermission = EncrypTool.dencode(s);
			TblOaAuthRolePermission rp = new TblOaAuthRolePermission();
			rp.setRoleId(new BigDecimal(roleId));
			rp.setPermissionId(new BigDecimal(realPermission));
			tblOaAuthRolePermissionDAO.insert(rp);
		}
		return 0;
	}

	@Override
	public TblOaAuthRole selectRoleByDesc(String roleDesc) {
		return tblOaAuthRoleDAO.selectByRoleDesc(roleDesc);
	}

	@Override
	public TblOaAuthRole selectRoleByDescAndSiteId(String roleDesc, String siteId) {
		return tblOaAuthRoleDAO.selectByRoleDescAndSiteId(roleDesc, siteId);
	}

	@Override
	public List<TblOaAuthRole> selectRoleList(String orderByField, String orderByMode, String roleDesc, int begin,
			int pageSize) {
		// 1.获取角色列表
		List<TblOaAuthRole> roleList = tblOaAuthRoleDAO.selectRoleListByQuery(orderByField, orderByMode, roleDesc,
				begin, pageSize);

		// 2.获取权限
		List roleIds = new ArrayList<String>();
		for (TblOaAuthRole role : roleList) {
			roleIds.add(role.getRoleId() + "");
		}
		List<TblOaAuthPermission> permissionList = tblOaAuthPermissionDAO.selectByRoleIds(roleIds);

		// 3.合并权限数据到角色列表
		for (TblOaAuthRole role : roleList) {
			String permissionNames = "";
			for (TblOaAuthPermission p : permissionList) {
				if ((role.getRoleId() + "").equalsIgnoreCase((p.getRoleId() + ""))) {
					permissionNames = permissionNames + "," + p.getPermissionName();
				}
			}
			permissionNames = permissionNames.replaceFirst(",", "");
			role.setPermissionNames(permissionNames);
		}
		return roleList;
	}

	@Override
	public int selectRoleListCount(String roleDesc) {
		return tblOaAuthRoleDAO.selectRoleListByQueryCount(roleDesc);
	}

	@Override
	public int deleteRoleByRoleId(String roleId) {
		// 1.删除角色权限关系表
		int i1 = tblOaAuthRolePermissionDAO.deleteByRoleId(roleId);
		// 2.删除角色
		int i2 = tblOaAuthRoleDAO.deleteByPrimaryKey(new BigDecimal(roleId));
		return 0;
	}

	@Override
	public TblOaAuthRole selectRoleByRoleId(String roleId) {
		TblOaAuthRole role = tblOaAuthRoleDAO.selectByPrimaryKey(new BigDecimal(roleId));
		if (null == role) {
			return null;
		}

		// 2.获取权限
		List roleIds = new ArrayList<String>();
		roleIds.add(role.getRoleId() + "");
		List<TblOaAuthPermission> permissionList = tblOaAuthPermissionDAO.selectByRoleIds(roleIds);
		if (!CollectionUtils.isEmpty(permissionList)) {
			// 3.合并权限数据到角色列表
			String permissionIds = "";
			for (TblOaAuthPermission p : permissionList) {
				if ((role.getRoleId() + "").equalsIgnoreCase((p.getRoleId() + ""))) {
					permissionIds = permissionIds + "," + p.getPermissionId();
				}
			}
			permissionIds = permissionIds.replaceFirst(",", "");
			role.setPermissionIds(permissionIds);
		}
		return role;
	}

	@Override
	public int updateRole(String roleId, String roleDesc, String permissions) {

		// 1.更新角色表
		TblOaAuthRole role = tblOaAuthRoleDAO.selectByPrimaryKey(new BigDecimal(roleId));
		role.setRoleDesc(roleDesc);
		tblOaAuthRoleDAO.updateByPrimaryKeySelective(role);

		// 2.获取都包含的权限
		List<String> bothPermissions = new ArrayList<String>();
		String newPs[] = permissions.split(",");
		List<String> newPermissions = new ArrayList<String>();
		for (String s : newPs) {
			newPermissions.add(EncrypTool.dencode(s));
		}
		List<String> roleIds = new ArrayList<String>();
		roleIds.add(roleId);
		List<TblOaAuthPermission> existPerimissions = tblOaAuthPermissionDAO.selectByRoleIds(roleIds);

		for (String ps : newPermissions) {
			for (TblOaAuthPermission p : existPerimissions) {
				if (ps.equalsIgnoreCase(p.getPermissionId() + "")) {
					bothPermissions.add(ps);
				}
			}
		}

		// 3.增加的
		for (String ps : newPermissions) {
			if (!bothPermissions.contains(ps)) {
				TblOaAuthRolePermission rp = new TblOaAuthRolePermission();
				rp.setRoleId(role.getRoleId());
				rp.setPermissionId(new BigDecimal(ps));
				tblOaAuthRolePermissionDAO.insert(rp);
			}
		}

		// 4.减少的
		for (TblOaAuthPermission p : existPerimissions) {
			if (!bothPermissions.contains(p.getPermissionId() + "")) {
				tblOaAuthRolePermissionDAO.deleteByRoleIdAndPermissionId(roleId, p.getPermissionId() + "");
			}
		}

		return 0;

	}

	@Override
	public int updateRole4Check(String roleId, String roleDesc, String permissions, String siteId) {

		// 1.更新角色表
		TblOaAuthRole role = tblOaAuthRoleDAO.selectByPrimaryKey(new BigDecimal(roleId));
		role.setRoleDesc(roleDesc);
		role.setSiteId(siteId.trim());
		tblOaAuthRoleDAO.updateByPrimaryKeySelective(role);

		// 2.获取都包含的权限
		List<String> bothPermissions = new ArrayList<String>();
		String newPs[] = permissions.split(",");
		List<String> newPermissions = new ArrayList<String>();
		for (String s : newPs) {
			newPermissions.add(EncrypTool.dencode(s));
		}
		List<String> roleIds = new ArrayList<String>();
		roleIds.add(roleId);
		List<TblOaAuthPermission> existPerimissions = tblOaAuthPermissionDAO.selectByRoleIds(roleIds);

		for (String ps : newPermissions) {
			for (TblOaAuthPermission p : existPerimissions) {
				if (ps.equalsIgnoreCase(p.getPermissionId() + "")) {
					bothPermissions.add(ps);
				}
			}
		}

		// 3.增加的
		for (String ps : newPermissions) {
			if (!bothPermissions.contains(ps)) {
				TblOaAuthRolePermission rp = new TblOaAuthRolePermission();
				rp.setRoleId(role.getRoleId());
				rp.setPermissionId(new BigDecimal(ps));
				tblOaAuthRolePermissionDAO.insert(rp);
			}
		}

		// 4.减少的
		for (TblOaAuthPermission p : existPerimissions) {
			if (!bothPermissions.contains(p.getPermissionId() + "")) {
				tblOaAuthRolePermissionDAO.deleteByRoleIdAndPermissionId(roleId, p.getPermissionId() + "");
			}
		}

		return 0;

	}

	@Override
	public int insertByCheck(String username) {
		TblOaAuthUser user = tblOaAuthUserDAO.selectByUserName(username);
		if (null != user) {
			return 0;
		} else if (null == user) {
			TblOaAuthUser authUser = new TblOaAuthUser();
			authUser.setUsername(username);
			authUser.setAddTime(new Date());
			int userId = tblOaAuthUserDAO.insert(authUser);
			// 加入tbl_oa_auth_user_role,设置默认角色DEFAULT_ROLE
			// 获取DEFAULT_ROLE角色的ID
			TblOaAuthRole defaultRole = tblOaAuthRoleDAO.selectRoleByRoleName(Constants.DEFAULT_ROLE);
			if (null != defaultRole) {
				TblOaAuthUserRole userRole = new TblOaAuthUserRole();
				userRole.setBizId(new BigDecimal(userId));
				userRole.setRoleId(defaultRole.getRoleId() + "");
				tblOaAuthUserRoleDAO.insert(userRole);
			}
		}
		return 0;
	}

	@Override
	public int updateUser4Check(LeaveForm form) {
		// 1.插入ts_user
		TsUser tsUser = new TsUser();
		tsUser.setUserid(form.getUserid());
		tsUser.setUsername(form.getUsername());
		tsUser.setPassword("C5E4C16E03EA");
		tsUser.setSiteid("769002");
		tsUser.setEnabled(new BigDecimal(0));
		tsUser.setCreatedate(new Date());
		tsUser.setCreateDateStr(DateFormatUtils.format(tsUser.getCreatedate(), "yyyy-MM-dd HH:mm:ss"));
		// tsUserDAO.insert(tsUser);
		// 2.插入auth_user
		TblOaAuthUser authUser = new TblOaAuthUser();
		authUser.setUsername(form.getUserid());
		authUser.setEnabled("1");
		authUser.setAddTime(new Date());
		String name = SecurityContextHolder.getContext().getAuthentication().getName();
		authUser.setAddUser(name);
		int bizId = 0;
		bizId = tblOaAuthUserDAO.insert(authUser);
		// 3.插入auth_user_role可能有多条记录产生
		String roles = form.getRoles();
		String roleArr[] = roles.split(",");
		for (String role : roleArr) {
			String realRole = EncrypTool.dencode(role);
			TblOaAuthUserRole userRole = new TblOaAuthUserRole();
			userRole.setBizId(new BigDecimal(bizId));
			userRole.setRoleId(realRole);
			tblOaAuthUserRoleDAO.insert(userRole);
		}
		return 0;
	}

	@Override
	public int insertDevice(TblCheckDevice device) {
		tblCheckDeviceDAO.insertSelective(device);
		return 0;
	}

	@Override
	public List<TblCheckDevice> selectDevice(List<String> siteList) {
		return tblCheckDeviceDAO.selectBySiteList(siteList);
	}

	@Override
	public List<TblCheckDevice> selectDevices(int begin, int pageSize,
			String siteid, String sn, String status) {
		return tblCheckDeviceDAO.selectDevices(begin, pageSize, siteid, sn, status);
	}

	@Override
	public int selectDeviceCount(String siteid, String sn, String status) {
		return tblCheckDeviceDAO.selectCount(siteid, sn, status);
	}

	@Override
	public int deleteDevice(Long id) {
		return tblCheckDeviceDAO.deleteByPrimaryKey(id);
	}

	@Override
	public int insertCmds(List<TblCheckCmd> cmds) {
		return tblCheckCmdDAO.insertBatch(cmds);
	}

	@Override
	public int insertCmd(TblCheckCmd cmd) {
		tblCheckCmdDAO.insertSelective(cmd);
		return 0;
	}
}
